package com.adnaan.restaurant.controller;

import com.adnaan.restaurant.common.untils.PageResult;
import com.adnaan.restaurant.common.untils.R;
import com.adnaan.restaurant.entity.CateGory;
import com.adnaan.restaurant.entity.Evaluation;
import com.adnaan.restaurant.entity.ResShop;
import com.adnaan.restaurant.service.IResShopService;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * author: adnaan
 * create: 2023-12-03 17:48
 * Description:
 */
@Tag(name = "店铺接口", description = "店铺相关功能")
@RestController
@RequestMapping("/restaurant/shop")
public class ResShopController {

    @Autowired
    private IResShopService resShopService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "店铺列表")
    @GetMapping("/list")
    public R list(@RequestParam Integer pageNum,
                  @RequestParam Integer pageSize,
                  @RequestParam String shopName) {

        PageResult list = resShopService.selectResShopList(shopName, pageNum, pageSize);

        return R.success(list);
    }

    @Operation(summary = "店铺详情")
    @GetMapping("/detail")
    public R detail(@RequestParam Long id) {
        String json = stringRedisTemplate.opsForValue().get(id.toString());
        ResShop resShop = new ResShop();
        if (json == null) {
            resShop = resShopService.selectResShopById(id);
            String stringStr = JSON.toJSONString(resShop);
            stringRedisTemplate.opsForValue().set(id.toString(), stringStr, 1, TimeUnit.HOURS);
        } else {
            resShop = JSON.parseObject(json, ResShop.class);
        }
        return R.success(resShop);
    }

    @Operation(summary = "菜品分类")
    @GetMapping("/category")
    public R category(@RequestParam Long shopId) {
        List<CateGory> list = resShopService.selectCateGoryByShopId(shopId);
        return R.success(list);
    }

    @Operation(summary = "热门店铺")
    @GetMapping("/hotShop")
    public R hotShop() {
        List<ResShop> list = resShopService.hotShop();
        return R.success(list);
    }

    @Operation(summary = "评价订单")
    @PostMapping("/evaluate")
    public R evaluate(@RequestBody Evaluation evaluation) {
        int evalStatus = resShopService.evaluate(evaluation);
        if (evalStatus > 0) {
            return R.success("评价成功");
        } else {
            return R.error(500, "评价失败");
        }
    }

    @Operation(summary = "评价列表")
    @GetMapping("/evalList")
    public R evalList(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam Long shopId) {
        PageResult list = resShopService.evalList(pageNum, pageSize, shopId);
        return R.success(list);
    }
}
